/**
 * @description: 获取请求域名
 * @param {any} app
 * @return {*}
 */
export const getUrl = (app: any, url: string) => {
    const domain = app?.proxy?.$domainApi ?? (window as any)?.domainApi;
    return `${domain}${url}`;
};

/**
 * 判断是否为空
 * @returns
 */
export const IsNotEmpty = str => {
    const type = Object.prototype.toString.call(str);
    switch (type) {
        case '[object String]':
        case '[object Number]':
        case '[object Boolean]':
        case '[object Undefined]':
        case '[object Null]':
            return !!str;
        case '[object Object]':
            return Object.keys(str).length > 0;
        case '[object Array]':
            return str.length > 0;
        default:
            return true;
    }
};

/**
 * 根据key，返回数据的key值
 * @returns
 */
export const getSetionId = (key: string, data: any[]) => {
    const arr: any[] = [];
    if (data.length > 0) {
        data.forEach((item: any) => {
            if (item?.hasOwnProperty(key)) {
                arr.push(item[key] as never);
            }
        });
    }
    return arr;
};

/**
 * @description: 判断文字内容是否超出容器
 * @param e
 * @param type type:2-宽度或者高度超过都算溢出,适用于多行溢出
 * @returns
 */
export const isTextBeyond = (e: any, type?: number) => {
    const ev = window.event || e;
    const textRange = el => {
        const textContent = el;
        const targetW = textContent.getBoundingClientRect().width;
        const targetH = textContent.getBoundingClientRect().height;
        const range = document.createRange();
        range.setStart(textContent, 0);
        range.setEnd(textContent, textContent.childNodes.length);
        const rangeWidth = range.getBoundingClientRect().width;
        const rangeHeight = range.getBoundingClientRect().height;
        if (type == 2) {
            return rangeWidth > targetW || rangeHeight > targetH;
        } else {
            return rangeWidth > targetW;
        }
    };
    return !textRange(ev.target);
};

/**
 * @description: 校验数据类型
 * @param {*} param1
 * @return {*}
 */
export const type = (() => {
    const toString = Object.prototype.toString;
    const dataType = [
        'Number',
        'String',
        'Boolean',
        'Array',
        'Object',
        'Symbol',
        'Undefined',
        'Null',
        'Function',
        'Date',
        'RegExp',
        'Error',
        'Promise'
    ];
    const type: any = {};
    for (const typeName of dataType) {
        const fnName = `is${typeName}`;
        !type[fnName] &&
            (type[fnName] = (value: any) => {
                const checkResult = `[object ${typeName}]`;
                return toString.call(value) === checkResult;
            });
    }
    type.isNumberic = (n: any) => {
        return !Number.isNaN(parseFloat(n)) && Number.isFinite(Number(n));
    };
    return type;
})();

/**
 * @description: 获取视频封面
 * @param {string} url
 * @param {number} w
 * @param {number} h
 * @return {*}
 */
export const getVideoImage = (url: string, w?: number, h?: number) => {
    return `${url}?x-oss-process=video/snapshot,t_0,f_jpg,w_${w},h_${h},m_fast`;
};

/**
 * 获取图片的宽度和高度
 * @param file
 * @returns
 */
export const getImageWH = file => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (e: any) {
            const img = new Image();
            img.src = e.target.result;
            img.onload = async function () {
                const W = img.width;
                const H = img.height;
                resolve({ W, H });
            };
        };
    });
};

/**
 * 防抖debounce
 * @param {any} func
 * @param {number} time
 */
export const debounce = (func: any, time = 300) => {
    let timer: any = null;
    return () => {
        timer && clearTimeout(timer);
        timer = setTimeout(() => {
            func.apply(null);
            timer = null;
        }, time);
    };
};
